﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Aliyeye.Car.Net.UI.SystemHelp;
using Aliyeye.Car.Net.UI.PopupDialog;
using Aliyeye.Car.Net.UI.Common;

namespace Aliyeye.Car.Net.UI.CarModel
{
    public partial class AddIllegal : BaseForm
    {
        public Aliyeye.Car.Net.UI.IllegalSvc.SoapHeaderClass sopaHeaderIllegal = new Aliyeye.Car.Net.UI.IllegalSvc.SoapHeaderClass();

        private bool isDriver = false;
        private int RepId = 0;
        private string pageinfo = null;
        public AddIllegal()
        {
            InitializeComponent();
        }
        public AddIllegal(bool isDri)
        {
            InitializeComponent();
            isDriver = isDri;
        }

        public AddIllegal(bool isDri, int id, string edit)
        {
            InitializeComponent();
            isDriver = isDri;
            RepId = id;
            pageinfo = edit;
        }
        private void AddIllegal_Load(object sender, EventArgs e)
        {
            //BLL.Archives bll = new BLL.Archives();
            //bll.ComBoxMT(cmbmeiti, "主管单位");
            //bll.ComBoxMT(cmbtype, "违章类型");

            BindComboBoxInfo();
            if (RepId != 0)
            {
                BindInfo();
                btnCancel.Text = "返回";
            }
            else
            {
                txtuser.Text = Login.LogUserName.ToString();
            }
        }

        private void BindComboBoxInfo()
        {
            overlay.FadeIn();Cursor.Current = Cursors.WaitCursor;
            ArchivesSvc.SoapHeaderClass sopaHeader = new ArchivesSvc.SoapHeaderClass();

            DataSet dsMT = null;

            ServiceManipulator.SafeAsyncExecution<ArchivesSvc.ArchivesServiceSoapClient>(
                proxy => proxy.ComBoxMT(sopaHeader),
                result =>
                {
                    if (result.Error == null)
                    {
                        dsMT = (DataSet)result.Result;
                        cmbmeiti.DisplayMember = "Namevalue";
                        cmbmeiti.ValueMember = "Id";
                        cmbmeiti.DataSource = dsMT.Tables[0];
                        cmbmeiti.SelectedIndex = 0;

                        overlay.FadeOut();Cursor.Current = Cursors.Default;
                    }
                    else
                    {
                        PageCommon.Error(result.Error.Message); overlay.FadeOut();Cursor.Current = Cursors.Default;
                    }
                },
                this
            );

            DataSet dsIllegal = null;
            ServiceManipulator.SafeAsyncExecution<ArchivesSvc.ArchivesServiceSoapClient>(
                proxy => proxy.ComBoxIllegal(sopaHeader),
                result =>
                {
                    if (result.Error == null)
                    {
                        dsIllegal = (DataSet)result.Result;
                        cmbtype.DisplayMember = "Namevalue";
                        cmbtype.ValueMember = "Id";
                        cmbtype.DataSource = dsIllegal.Tables[0];
                        cmbtype.SelectedIndex = 0;

                        overlay.FadeOut();Cursor.Current = Cursors.Default;
                    }
                    else
                    {
                        PageCommon.Error(result.Error.Message); overlay.FadeOut();Cursor.Current = Cursors.Default;
                    }
                },
                this
            );
        }

        private void BindInfo()
        {
            //BLL.Illegal bll = new BLL.Illegal();
            //Model.Illegal model = bll.GetModel(RepId);
            //txtdriver.Text = model.Driver.ToString();
            //txtaddr.Text = model.Addr.ToString();
            //txtcarcode.Text = model.CarCode.ToString();
            //txtCents.Text = model.Cents.ToString();
            //txtHour.Text = model.Hours.ToString();
            //txtmarks.Text = model.Marks.ToString();
            //txtPrie.Text = model.Price.ToString();
            //txttime.Value = model.Time;
            //cmbmeiti.SelectedText = model.MeiTi.ToString();
            //cmbtype.SelectedText = model.Type.ToString();

            //BLL.Accounts_Users userbll = new BLL.Accounts_Users();
            //Model.Accounts_Users usermodel = userbll.GetModel(model.UserId);
            //if (model != null)
            //{
            //    txtuser.Text = usermodel.TrueName.ToString();
            //}

            overlay.FadeIn();Cursor.Current = Cursors.WaitCursor;
            IllegalSvc.Illegal model = new IllegalSvc.Illegal();
            ServiceManipulator.SafeAsyncExecution<IllegalSvc.IllegalServiceSoapClient>(
                proxy => proxy.GetModel(sopaHeaderIllegal, RepId),
                result =>
                {
                    if (result.Error == null)
                    {
                        model = (IllegalSvc.Illegal)result.Result;
                        txtdriver.Text = model.Driver.ToString();
                        txtaddr.Text = model.Addr.ToString();
                        txtcarcode.Text = model.CarCode.ToString();
                        txtCents.Text = model.Cents.ToString();
                        txtHour.Value = model.Time; //model.Hours.ToString();
                        txtmarks.Text = model.Marks.ToString();
                        txtPrie.Text = model.Price.ToString();
                        txttime.Value = model.Time;
                        cmbmeiti.Text = model.MeiTi.ToString();
                        cmbtype.Text = model.Type.ToString();
                        txtuser.Text = model.InputTrueName;

                        overlay.FadeOut();Cursor.Current = Cursors.Default;
                    }
                    else
                    {
                        PageCommon.Error(result.Error.Message); overlay.FadeOut();Cursor.Current = Cursors.Default;
                    }
                },
                this
            );

            if (pageinfo == "look")
            {
                txtaddr.Enabled = false;
                txtcarcode.Enabled = false;
                txtCents.Enabled = false;
                txtdriver.Enabled = false;
                txtHour.Enabled = false;
                txtmarks.Enabled = false;
                txtPrie.Enabled = false;
                txttime.Enabled = false;
                txtuser.Enabled = false; 
                btnSave.Visible = false;
                cmbmeiti.Enabled = false;
                cmbtype.Enabled = false;
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtcarcode.Text))
            {
                DialogWarning dia = new DialogWarning("车辆编号不能为空！");
                dia.ShowDialog();
                return;
            }

            if (string.IsNullOrEmpty(txtPrie.Text))
            {
                txtPrie.Text = "0";
            }
            overlay.FadeIn();Cursor.Current = Cursors.WaitCursor;
            //BLL.Illegal bll = new BLL.Illegal();
            IllegalSvc.Illegal model = new IllegalSvc.Illegal();

            model.CarCode = txtcarcode.Text;
            DateTime dt = new DateTime(txttime.Value.Year, txttime.Value.Month, txttime.Value.Day, txtHour.Value.Hour, txtHour.Value.Minute, txtHour.Value.Second);
            model.Time = dt;
            model.Addr = txtaddr.Text;
            model.MeiTi = cmbmeiti.Text;
            model.Type = cmbtype.Text;

            model.Price = decimal.Parse(txtPrie.Text);
            model.Marks = txtmarks.Text;
            model.UserId = Login.LogUserId;
            model.Inputime = DateTime.Now;
            model.Hours = txtHour.Text;
            model.Driver = txtdriver.Text;
            model.Cents = txtCents.Text;
            if (RepId > 0)
            {
                model.Id = RepId;
                //bll.Update(model);

                //DialogNotify diaSecc = new DialogNotify("违章信息修改成功！");
                //diaSecc.ShowDialog();

                ServiceManipulator.SafeAsyncExecution<IllegalSvc.IllegalServiceSoapClient>(
                  proxy => proxy.Update(sopaHeaderIllegal, model),
                  result =>
                  {
                      if (result.Error == null)
                      {
                          PageCommon.Notify("违章信息修改成功！");
                          overlay.FadeOut();Cursor.Current = Cursors.Default;
                      }
                      else
                      {
                          PageCommon.Error(result.Error.Message); overlay.FadeOut();Cursor.Current = Cursors.Default;
                      }
                  },
                  this
                  );
            }
            else
            {
                //bll.Add(model);
                //DialogNotify diaSecc = new DialogNotify("违章信息添加成功！");
                //diaSecc.ShowDialog();

                ServiceManipulator.SafeAsyncExecution<IllegalSvc.IllegalServiceSoapClient>(
              proxy => proxy.Add(sopaHeaderIllegal, model),
              result =>
              {
                  if (result.Error == null)
                  {
                      PageCommon.Notify("违章信息添加成功！");
                      overlay.FadeOut();Cursor.Current = Cursors.Default;
                  }
                  else
                  {
                      PageCommon.Error(result.Error.Message);
                  }
              },
              this
          );
            }
            clear();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void clear()
        {
            txtaddr.Text = "";
            txtcarcode.Text = "";
            txtCents.Text = "";
            txtdriver.Text = "";
            txtHour.Text = "";
            txtmarks.Text = "";
            txtPrie.Text = "";
        }
        private bool IsNumberMoney(System.Windows.Forms.TextBox t, KeyPressEventArgs e)
        {
            if ((e.KeyChar >= '0' && e.KeyChar <= '9' || e.KeyChar == '.' || e.KeyChar == 8 || e.KeyChar == 13))
            {
                //lb.Text=string.Empty;
                return true;
            }
            else
            {
                DialogNotify diaSecc = new DialogNotify("只能输入数字！");
                diaSecc.ShowDialog();
                e.Handled = true;
                return false;
            }
        }
        private void txtPrie_KeyPress(object sender, KeyPressEventArgs e)
        {
            IsNumberMoney(txtPrie, e);
        }

        #region 车牌号 输入框数据绑定

        string driverName = string.Empty;
        private void txtCarCode_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                string strSearch = this.txtcarcode.Text;
                if (string.IsNullOrEmpty(strSearch))
                {
                    DialogWarning dia = new DialogWarning("车牌号不能为空！");
                    dia.ShowDialog();
                    return;
                }
                else
                {
                    DisplayCustomeLIst(strSearch, "车牌号");
                }
            }
        }

        private void DisplayCustomeLIst(string strSearch, string type)
        {
            if (string.IsNullOrEmpty(strSearch))
            {
                return;
            }
            string strSelectWhere = "";
            if (type == "车牌号")
            {
                strSelectWhere = " Code like '%" + strSearch + "%'";
            }
            overlay.FadeIn();Cursor.Current = Cursors.WaitCursor;
            DataSet ds = null;
            this.dgvDriverInfo.DataSource = null;
            dgvDriverInfo.AutoGenerateColumns = false;
            CarSvc.SoapHeaderClass sopaHeader = new Aliyeye.Car.Net.UI.CarSvc.SoapHeaderClass();
            ServiceManipulator.SafeAsyncExecution<CarSvc.CarServiceSoapClient>(
                proxy => proxy.GetList(sopaHeader, strSelectWhere),
                result =>
                {
                    if (result.Error == null)
                    {
                        ds = (DataSet)result.Result;
                        if (ds != null && ds.Tables[0].Rows.Count > 0)
                        {
                            dgvDriverInfo.DataSource = ds.Tables[0];
                            this.dgvDriverInfo.Visible = true;
                            this.dgvDriverInfo.Focus();
                        }
                        else
                        {
                            PageCommon.Alert("查找的车辆不存在，请重新查询！");
                            txtcarcode.Text = driverName;
                        }
                        overlay.FadeOut();Cursor.Current = Cursors.Default;
                    }
                    else
                    {
                        PageCommon.Error(result.Error.Message); overlay.FadeOut();Cursor.Current = Cursors.Default;
                    }
                },
                this
            );

        }

        private void dgvDriveInfo_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            if (this.dgvDriverInfo.Rows.Count != 0)
            {
                for (int i = 0; i < this.dgvDriverInfo.Rows.Count; )
                {
                    this.dgvDriverInfo.Rows[i].DefaultCellStyle.BackColor = System.Drawing.Color.PapayaWhip;
                    i += 2;
                }
            }
        }

        private void dgvInfo_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Return)
            {
                this.txtcarcode.Text = this.dgvDriverInfo.CurrentRow.Cells["车牌号1"].Value.ToString();
                //  driverID = Convert.ToInt32(this.dgvInfo.CurrentRow.Cells["编号"].Value.ToString());
                driverName = this.txtcarcode.Text;
                this.dgvDriverInfo.Visible = false;
                this.txttime.Focus();
            }
        }

        private void dgvInfo_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {

            e.CellStyle.WrapMode = DataGridViewTriState.True;
            if (this.dgvDriverInfo.Columns[e.ColumnIndex].Name == "性别")
            {
                if (e.Value != null)
                {
                    if (Convert.ToBoolean(e.Value))
                    {
                        e.Value = "带驾";
                    }
                    else
                    {
                        e.Value = "自驾";
                    }
                }
            }

        }

        #endregion 车牌号 输入框数据绑定

    }
}
